/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.util;

import com.beyu.internalws.AgentSmsApi_Service;
import com.fibo.internalws.Service;

/**
 *
 * @author Nguyen Hung
 */
public class SMSUTIL {

    public static String sendSmSUtil(String message, String receiver) {
        String messageResult = null;
        AgentSmsApi_Service agent = new AgentSmsApi_Service();
        int type = 1; //1: notify message, 0: Advertise message
        String result = agent.getAgentSmsApiServiceImplPort().sendSms(PackageContants.authenticate_user, PackageContants.authenticate_pass, PackageContants.bran_name, message, receiver, type);
        System.out.println("-----------RESULT BEYU--------" + result);
        if (result != null) {
            if (result.equals("-13")) {
                messageResult = "Invaild receiver.";
            } else if (result.equals("-10")) {
                messageResult = "Missing username";
            } else if (result.equals("-11")) {
                messageResult = "Missing password";
            } else if (result.equals("-8")) {
                messageResult = "Missing brand name";
            } else if (result.equals("-7")) {
                messageResult = "Missing sms text";
            } else if (result.equals("-12")) {
                messageResult = "Message length too long to allowed";
            } else if (result.equals("-6")) {
                messageResult = "Missing destination address";
            } else if (result.equals("-5")) {
                messageResult = "Lỗi User";
            } else if (result.equals("-4")) {
                messageResult = "Invalid brand name";
            } else if (result.equals("-3")) {
                messageResult = "Invalid type";
            } else if (result.equals("-15")) {
                messageResult = "Not enough credits";
            } else if (result.equals("-20")) {
                messageResult = "Không đúng nội dung theo quy định";
            } else if (result.equals("-21")) {
                messageResult = "IP này không hỗ trợ gửi tin nhắn!";
            } else if (result.equals("-22")) {
                messageResult = "Qua thời gian gửi SMS";
            } else {
                int i = result.indexOf("|");
                if (i > 0) {
                    String reString = result.substring(0, i);
                    if (reString.equals("-14")) {
                        messageResult = "Message contain forbidden words.";
                    } else if (reString.equals("-9")) {
                        messageResult = "Message submit failed because the phone number invalid format.";
                    } else if (reString.equals("1")) {
                        messageResult = "SMS SUCCESS";
                    } else {
                        messageResult = "SMS NO SUCCESS";
                    }
                }
            }
        }
        return messageResult;

    }

    public static String sendSmSUtilFibo(String numberPhone, String contentmessage) {
        String messageResult = null;
        Service ser = new Service();
       
        String result = ser.getServiceSoap().sendSMS(PackageContants.clientNo, PackageContants.clientPass, numberPhone, contentmessage, PackageContants.smsGUID, Integer.parseInt(PackageContants.serviceType));
        if (result != null) { 
            
            int t = "<Code>".length();
            int n = result.trim().indexOf("<Code>");
            int m = result.trim().indexOf("</Code>");
            String a = result.substring(n + t, m);
           
            if(a.equals("200")){
                  messageResult = "SMS SUCCESS";
            }else if(a.equals("100")){
                 messageResult = "Please check your account!";
            }else if(a.equals("102")){
                 messageResult = "Your balance is zero!";
            }else if(a.equals("101")){
                 messageResult = "Please check your Phone number!";
            }else if(a.equals("103")){
                 messageResult = "Service type is wrong!";
            }
        }
        return messageResult;

    }
    
    // hàm nhân tin nhan cua khach hang
    public static String reviceSMSofCustomer(Long fromId) {
        String messageResult = null;
        Service ser = new Service();
       System.out.println("--------------"+PackageContants.clientNo+ PackageContants.clientPass+ fromId+Integer.parseInt(PackageContants.serviceType));
        String result = ser.getServiceSoap().getCommingSMSListFromID(PackageContants.clientNo, PackageContants.clientPass, fromId,Integer.parseInt(PackageContants.serviceType));

//        if (result != null) { 
//            System.out.println("------------a--"+result);
//            int t = "<Code>".length();
//            int n = result.trim().indexOf("<Code>");
//            int m = result.trim().indexOf("</Code>");
//            String a = result.substring(n + t, m);
//           
//            if(a.equals("301")){
//                  messageResult = "NO SMS";
//            }else if(a.equals("100")){
//                 messageResult = "Please check your account!";
//            }else {
//                 messageResult = "DANH SACH SMS-----------------"+result;
//            }
//        }
        return result;

    }
}
